package com.phyane.common.baseopp;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DataBaseOpp {
	
	public static Connection getConnection() throws Exception{
		String bridge ="com.mysql.jdbc.Driver";
        String mysql ="jdbc:mysql://localhost:3306/phyane";
        String user ="root";
        String passwd ="123456";
        Connection conn = null;
        Class.forName(bridge);
        conn = DriverManager.getConnection(mysql,user,passwd);
        return conn;
	}
	
	public static void closeConn(Connection conn){
		try{
			conn.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void insertPicIntoDB(String picPath) throws Exception {
		Connection conn = DataBaseOpp.getConnection();
		String sql = "insert into main_content_tb(id,picture,text,auth_id) values(null,?,?,1)";
		PreparedStatement ps = conn.prepareStatement(sql);
		String text = "This is the home of phyane.com, and this is a personal site without any"+
				" business propose, so please take it easy to come to communicate with us here";
		ps.setString(2, text);
		File pic = new File(picPath);
		InputStream isPic = new FileInputStream(pic);
		ps.setBinaryStream(1, isPic, (int)pic.length());
		System.out.println(ps.executeUpdate());
	}
}
